package com.tubruk.trackme;

import com.tubruk.object.Global;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;

public class ImportData extends Activity {
	private static final int BROWSE_FILES = 1;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.importdata);
        		
        ImageButton locate = (ImageButton) findViewById(R.id.locate);
		locate.setOnClickListener(new ActionClickListener(ImportData.this, TrackMapActivity.class));
		
		ImageButton search = (ImageButton) findViewById(R.id.search);
		search.setOnClickListener(new ActionClickListener(ImportData.this, History.class));
		
		ImageButton browse = (ImageButton) findViewById(R.id.browse);
		browse.setOnClickListener(new BrowseClickListener());
	}
	
	private class BrowseClickListener implements OnClickListener {
		
		public void onClick(View v) {
			Intent i = new Intent(ImportData.this, FileBrowser.class);
			//i.putExtra(FileBrowser.MODE_TYPE, FileBrowser.MODE_BROWSE);
			startActivityForResult(i, ImportData.BROWSE_FILES);
		}
		
	}
	
	@Override 
	public void onActivityResult(int requestCode, int resultCode, Intent data) {     
	  super.onActivityResult(requestCode, resultCode, data); 
	  switch(requestCode) { 
	    case (ImportData.BROWSE_FILES) : { 
	      if (resultCode == Activity.RESULT_OK) { 
	    	 
	    	 String path = data.getStringExtra(FileBrowser.RESULT_PATH);
	    	 EditText selected = (EditText) findViewById(R.id.searchtext);
	    	 selected.setText(path);
	    	 //selected.setText("As3m");
	      } 
	      break; 
	    } 
	  } 
	}
	
	private class ActionClickListener implements OnClickListener {
		private Class<?> target;
		private Context source;
		
		public ActionClickListener(Context source, Class<?> target){
			this.source = source;
			this.target = target;
		}
		
		public void onClick(View v) {
			finish();
			Intent i = new Intent(source, target);
			startActivity(i);
		}
		
	}
}
